package yifeng.gui;

import yifeng.gui.Elevator.Direction;

public class Controller extends Thread {
	private Elevator ele;
	
	public Controller(Elevator ele) {
		super();
		this.ele = ele;
	}
	
	@Override
	public void run() {
		while(true){
			lsleep(50);
			
			
			//
			if(ele.getDirection() == Direction.up){
				incrementTheFloorIndex();
				clearTheCommandsAndFocusOnTheFloor();
			}else if(ele.getDirection() == Direction.stop){
				ele.getFloorList().get(ele.getCurrentFloor()).setEnabled(true);
			}
			
			
			
			//
			
			
			
			
			
			
			
			
			
			
			
			
			
			boolean hasNextAction = false;
			//
			if(ele.getDirection() == Direction.stop){
				for(int i = 0; i < ele.getFloors(); i++){
					if(i == ele.getCurrentFloor()) 
						continue;
					if(ele.getUpCommands()[i] == 1 || ele.getFloorCommands()[i] == 1 ||
							ele.getDownCommands()[i] == 1){
						ele.setDirection(i > ele.getCurrentFloor() ? Direction.up : Direction.down);
						hasNextAction = true;
					}
				}
			} else if (ele.getDirection() == Direction.up) {
				hasNextAction = checkUpStairs();
				if (hasNextAction)
					leaveFromAFloorAndResetButton();
				else {
					hasNextAction = checkDownStairs();
					if (hasNextAction)
						leaveFromAFloorAndResetButton();
					else
						ele.setDirection(Direction.stop);
				}
			}else{
				hasNextAction = checkDownStairs();
				if(!hasNextAction)
					checkUpStairs();
				if(!hasNextAction)
					ele.setDirection(Direction.stop);
			}
			//
			
			
			//if it will continue to move then
			//leave a floor and move to the next floor
			if(hasNextAction){
				ele.getFloorList().get(ele.getCurrentFloor()).setEnabled(false);
				lsleep(1000);
			}
		}
	}

	private void clearTheCommandsAndFocusOnTheFloor() {
		int currentFloor = ele.getCurrentFloor();
		ele.getFloorCommands()[currentFloor] = 0;
		ele.getUpCommands()[currentFloor] = 0;
		ele.getFloorList().get(currentFloor).setEnabled(true);
		lsleep(500);
	}

	private void incrementTheFloorIndex() {
		int currentFloor = ele.getCurrentFloor();
		ele.setCurrentFloor(++currentFloor);
	}

	private void leaveFromAFloorAndResetButton() {
		int currentFloor = ele.getCurrentFloor();
		ele.getFloorList().get(currentFloor).setEnabled(false);
		ele.getButtonList().get(currentFloor).setEnabled(true);
		ele.getUpList().get(currentFloor).setEnabled(true);
	}

	private boolean checkDownStairs() {
		for(int i = ele.getCurrentFloor() - 1; i >= 0; i--){
			if(ele.getDownCommands()[i] == 1 || ele.getFloorCommands()[i] == 1){
				ele.setDirection(Direction.down);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * check if any command upstairs
	 * @return
	 */
	private boolean checkUpStairs() {
		for(int i = ele.getCurrentFloor() + 1; i < ele.getFloors(); i++){
			if(ele.getUpCommands()[i] == 1 || ele.getFloorCommands()[i] == 1){
				ele.setDirection(Direction.up);
				return true;
			}
		}
		return false;
	}

	
	private void lsleep(int millisec) {
		try {
			Thread.sleep(millisec);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
